package com.health.framework.dao;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;

import org.springframework.dao.DataAccessException;

@SuppressWarnings("unchecked")
public abstract class BaseDao<E, PK extends Serializable> extends MyHibernateDaoSupport implements IBaseDao<E, PK> {

    Class<E> clazz;

    public BaseDao() throws DataAccessException {
        clazz = (Class<E>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    }

    public E get(PK id) throws DataAccessException {
        return (E)super.getHibernateTemplate().get(clazz, id);
    }
    
    public List<E> getAll() throws DataAccessException {
        return super.getHibernateTemplate().find("from " + clazz.getSimpleName());
    }

    public void save(E e) throws DataAccessException {
        super.getHibernateTemplate().save(e);
    }

    public void update(E e) throws DataAccessException {
        super.getHibernateTemplate().update(e);
    }

    public void delete(PK id) throws DataAccessException {
        delete(get(id));
    }

    public void delete(E e) throws DataAccessException {
        super.getHibernateTemplate().delete(e);
    }
    
    public boolean isUnique(E e, String key) throws DataAccessException {
        throw new UnsupportedOperationException("Not supported yet!");
    }

}
